#!/bin/sh
# @Time    : 2020-12-17
# @Author  : TP
# @Name    : service_log_kill_tailf.sh
# @Description  :  用于检测日志里是否包含Started .* second 判断项目是否成功启动 配合tailf.sh一起使用


#项目日志文件路径
SERVICE_NAME=kc-admin
SERVICE_DIR=/home/opt/${SERVICE_NAME}
#日志文件
console_log=${SERVICE_DIR}/console.log

echo "项目启动日志路径：${console_log}"

second=0

echo "" > ${SERVICE_DIR}/kc_admin_started_log
echo "" > ${SERVICE_DIR}/kc_admin_jvm_log

while true
do
  sum=`cat ${console_log} | grep 'Started KCApplication in' | wc -l`
  sum_jvm=`cat ${console_log} | grep 'JVM running for' | wc -l`
  #调试用
  #echo "[tags] sum: ${sum} second: $second sum_jvm: ${sum_jvm}"
  echo "${sum}" > ${SERVICE_DIR}/kc_app_started_log
  echo "${sum_jvm}" > ${SERVICE_DIR}/kc_app_jvm_log
  if [ ${second} -ge 46 ];then
    echo "部署等待时间过长 退出部署"
    ps -ef | grep "tailf ${console_log}" | grep -v grep | awk '{print $2}' | xargs kill -9
    break
  fi

  if [ ${sum} -gt 0 ] || [ ${sum_jvm} -gt 0 ];then
    ps -ef | grep "tailf ${console_log}" | grep -v grep | awk '{print $2}' | xargs kill -9
    echo "[tags] 项目启动花费 ${second} 秒"
    break
  fi
  second=$((second + 2))
  sleep 2
  #调试用
  #echo "[tags] 项目启动花费 ${second} 秒"
done